<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="root"></div>
    <script src="./lib/react.js"></script>
    <script src="./lib/react_dom.js"></script>
    <script src="./lib/babel.js"></script>

    <script type="text/babel">
      /*
        this的四种绑定规则：
          1.默认绑定：独立执行foo()
          2.隐式绑定：被一个对象执行obj.foo() -> obj
          3.显示绑定：call/apply/bind foo.bind('ooo') -> String('ooo')
          4.new绑定：new Foo() -> 创建一个新对象，并赋值给this
      */
      class App extends React.Component {
        constructor() {
          super()
          this.state = {
            message: 'hello world',
            counter: 0,
          }
        }

        btn1Click() {
          console.log('btn1Click', this)
          this.setState({
            counter: this.state.counter + 1,
          })
        }

        // class fields
        name = 'abc'
        btn2Click = () => {
          console.log('btn1Click', this)
          this.setState({
            counter: 100,
          })
        }

        btn3Click() {
          this.setState({
            counter: 200,
          })
        }

        render() {
          const { message, counter } = this.state
          return (
            <div>
              <button onClick={this.btn1Click.bind(this)}>按钮1</button>
              <button onClick={this.btn2Click}>按钮二</button>
              {/* onClick的值需要是一个函数，那么我们可以传入一个回调函数，并且在回调函数中执行需要执行的方法 */}
              <button onClick={() => this.btn3Click()}>按钮三</button>
              <h2>当前计数{counter}</h2>
            </div>
          )
        }
      }

      const root = ReactDOM.createRoot(document.querySelector('#root'))
      root.render(<App />)
    </script>
  </body>
</html>
